// UserDefinedDisplayUpdateHandler.cpp: implementation of the UserDefinedDisplayUpdateHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "UserDefinedDisplayUpdateHandler.h"
#include "UserDefinedDisplayQueryHandler.h"
#include "DatabaseManager.h"
#include "UserDefinedDisplaySet.h"

#include "CommonLib/Structure.h"
#include "CommonLib/Packet.h"

#include "CommServerLib/CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UserDefinedDisplayUpdateHandler::UserDefinedDisplayUpdateHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

UserDefinedDisplayUpdateHandler::~UserDefinedDisplayUpdateHandler()
{
}

int UserDefinedDisplayUpdateHandler::Handling()
{
	UserDefinedDisplayUpdateStruct * pUserDefinedDisplayUpdateStruct = (UserDefinedDisplayUpdateStruct *)m_pPacket->GetpData();

	int nMethodAck;
	switch (pUserDefinedDisplayUpdateStruct->m_dwMethodType)
	{
	case VCS_MT_ADDUSERDEFINEDDISPLAY:
		nMethodAck = AddUserDefinedDisplay(pUserDefinedDisplayUpdateStruct->m_UserDefinedDisplayItem);
		break;
	case VCS_MT_MODIFYUSERDEFINEDDISPLAY:
		nMethodAck = ModifyUserDefinedDisplay(pUserDefinedDisplayUpdateStruct->m_UserDefinedDisplayItem);
		break;
	case VCS_MT_DELETEUSERDEFINEDDISPLAY:
		nMethodAck = DeleteUserDefinedDisplay(pUserDefinedDisplayUpdateStruct->m_UserDefinedDisplayItem);
		break;
	case VCS_MT_RENAMEUSERDEFINEDDISPLAY:
		nMethodAck = RenameUserDefinedDisplay(pUserDefinedDisplayUpdateStruct->m_UserDefinedDisplayItem);
		break;
	default:
		ASSERT(false);
		nMethodAck = VCS_MR_UNKNOWNMETHOD;
		break;
	}

	UserDefinedDisplayAckStruct * pUserDefinedDisplayAckStruct = UserDefinedDisplayQueryHandler::QueryUserDefinedDisplay(pUserDefinedDisplayUpdateStruct->m_UserDefinedDisplayItem.m_szUserId);
	pUserDefinedDisplayAckStruct->m_dwMethodType = pUserDefinedDisplayUpdateStruct->m_dwMethodType;
	pUserDefinedDisplayAckStruct->m_nMethodAck = nMethodAck;

	Packet packetAck(pUserDefinedDisplayAckStruct->m_dwBufSize, (LPBYTE)pUserDefinedDisplayAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pUserDefinedDisplayAckStruct;

	return 0;
}

int UserDefinedDisplayUpdateHandler::AddUserDefinedDisplay(const UserDefinedDisplayItem& uddi)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserDefinedDisplaySet rsUserDefinedDisplay(pDatabase);

	rsUserDefinedDisplay.m_strFilter.Format("UserId = '%s' AND DisplayName = '%s'", uddi.m_szUserId, uddi.m_szDisplayName);
	rsUserDefinedDisplay.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
	if (!rsUserDefinedDisplay.IsEOF())
	{
		nRet = VCS_MR_DISPLAYEXIST;
	}
	else
	{
		rsUserDefinedDisplay.AddNew();
		rsUserDefinedDisplay.m_UserId = uddi.m_szUserId;
		rsUserDefinedDisplay.m_DisplayName = uddi.m_szDisplayName;
		rsUserDefinedDisplay.m_ShareFlag = uddi.m_nShareFlag;
		rsUserDefinedDisplay.m_Content = uddi.m_szContent;
		rsUserDefinedDisplay.Update();
	}
	rsUserDefinedDisplay.Close();

	return nRet;
}

int UserDefinedDisplayUpdateHandler::ModifyUserDefinedDisplay(const UserDefinedDisplayItem& uddi)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserDefinedDisplaySet rsUserDefinedDisplay(pDatabase);

	rsUserDefinedDisplay.m_strFilter.Format("UserId = '%s' AND DisplayName = '%s'", uddi.m_szUserId, uddi.m_szDisplayName);
	rsUserDefinedDisplay.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
	if (rsUserDefinedDisplay.IsEOF())
	{
		nRet = VCS_MR_DISPLAYNOTEXIST;
	}
	else
	{
		rsUserDefinedDisplay.Edit();
		rsUserDefinedDisplay.m_ShareFlag = uddi.m_nShareFlag;
		rsUserDefinedDisplay.m_Content = uddi.m_szContent;
		rsUserDefinedDisplay.Update();
	}
	rsUserDefinedDisplay.Close();

	return nRet;
}

int UserDefinedDisplayUpdateHandler::DeleteUserDefinedDisplay(const UserDefinedDisplayItem& uddi)
{
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();

	CString strSql;
	strSql.Format("DELETE FROM UserDefinedDisplay WHERE UserId = '%s' AND DisplayName = '%s'", uddi.m_szUserId, uddi.m_szDisplayName);
	pDatabase->ExecuteSQL(strSql);

	return VCS_MR_OK;
}

int UserDefinedDisplayUpdateHandler::RenameUserDefinedDisplay(const UserDefinedDisplayItem& uddi)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserDefinedDisplaySet rsUserDefinedDisplay(pDatabase);

	if (stricmp(uddi.m_szContent, uddi.m_szDisplayName) != 0)
	{
		rsUserDefinedDisplay.m_strFilter.Format("UserId = '%s' AND DisplayName = '%s'", uddi.m_szUserId, uddi.m_szContent);
		rsUserDefinedDisplay.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
		if (!rsUserDefinedDisplay.IsEOF())
		{
			nRet = VCS_MR_DISPLAYEXIST;
		}
		else
		{
			CString strSql;
			strSql.Format("UPDATE UserDefinedDisplay SET DisplayName = '%s' WHERE UserId = '%s' AND DisplayName = '%s'", uddi.m_szContent, uddi.m_szUserId, uddi.m_szDisplayName);
			pDatabase->ExecuteSQL(strSql);
		}
		rsUserDefinedDisplay.Close();
	}
	else
	{
		CString strSql;
		strSql.Format("UPDATE UserDefinedDisplay SET DisplayName = '%s' WHERE UserId = '%s' AND DisplayName = '%s'", uddi.m_szContent, uddi.m_szUserId, uddi.m_szDisplayName);
		pDatabase->ExecuteSQL(strSql);
	}

	return nRet;
}